//------------------------------------------------------------------------------
// <copyright file="PositionProperties.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using DotBeer.Data.Properties;

//
// This class is modified implementation posted at:
// http://eastgroup.pl/post/Zapamietywanie-pozycji-oraz-rozmiaru-okna-w-WPFie.aspx
//

namespace DotBeer.UI.WPFClient
{
    public class PositionProperties
    {
        #region private members

        Window window;

        #endregion

        #region properties

        private string WindowPositionKey
        {
            get { return String.Format("{0}Position", this.window.GetType().Name); }
        }

        private string WindowStateKey
        {
            get { return String.Format("{0}State", this.window.GetType().Name); }
        }

        public static readonly DependencyProperty SavePositionProperty =
           DependencyProperty.RegisterAttached("SavePositionAndState",
                                               typeof(bool),
                                               typeof(PositionProperties),
                                               new PropertyMetadata(SavePosition_Changed));

        #endregion

        #region constructors

        private PositionProperties(Window window)
        {
            this.window = window;
            this.window.StateChanged += new EventHandler(window_StateChanged);
            this.window.LocationChanged += new EventHandler(window_LocationChanged);
            this.window.SizeChanged += new SizeChangedEventHandler(window_SizeChanged);
            this.window.Closing += new System.ComponentModel.CancelEventHandler(window_Closing);
            this.window.Initialized += new EventHandler(window_Initialized);
        }

        #endregion

        #region public methods

        public static void SetSavePositionAndState(DependencyObject d, bool save)
        {
            d.SetValue(SavePositionProperty, save);
        }

        public static bool GetSavePositionAndState(DependencyObject d)
        {
            return (bool)d.GetValue(SavePositionProperty);
        }

        #endregion

        #region private methods

        private static void SavePosition_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Window window = d as Window;

            if (window != null)
            {
                if ((bool)e.NewValue)
                {
                    PositionProperties positionProperties = new PositionProperties(window);
                }
            }
        }

        private void window_StateChanged(object sender, EventArgs e)
        {
            this.SaveState();
        }

        private void window_LocationChanged(object sender, EventArgs e)
        {
            this.SaveBounds();
        }

        private void window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.SaveBounds();
        }

        private void window_Initialized(object sender, EventArgs e)
        {
            Rect rect = Rect.Parse(WindowsPositionSettings.Default[this.WindowPositionKey].ToString().Replace(";", ","));

            if (rect != Rect.Empty)
            {
                if (rect.Left > SystemParameters.PrimaryScreenWidth)
                    this.window.Left = 0;
                else
                    this.window.Left = rect.Left;

                if (rect.Top > SystemParameters.PrimaryScreenHeight)
                    this.window.Top = 0;
                else
                    this.window.Top = rect.Top;

                this.window.Height = rect.Height;
                this.window.Width = rect.Width;
            }

            WindowState state = (WindowState)WindowsPositionSettings.Default[this.WindowStateKey];
            this.window.WindowState = state;
        }

        private void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.SaveBounds();
            this.SaveState();
        }

        private void SaveState()
        {
            if (this.window.WindowState != WindowState.Minimized)
                WindowsPositionSettings.Default[this.WindowStateKey] = this.window.WindowState;

            WindowsPositionSettings.Default.Save();
        }

        private void SaveBounds()
        {
            WindowsPositionSettings.Default[this.WindowPositionKey] = this.window.RestoreBounds;
            WindowsPositionSettings.Default.Save();
        }

        #endregion
    }
}
